<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

class SignInForm extends HTML_QuickForm {
  private $tpl;
  private $user;

  function SignInForm($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;

    /**
     * Set cookie for use with javascript.md5
     */

    if (!isset($_COOKIE['sess'])) {
      $better_token = md5(uniqid(rand(), true));
      setcookie('sess', $better_token, 0, '/');
    }

    parent::HTML_QuickForm('login','post','','','onsubmit="javascript:password.value=hex_hmac_sha1(hex_sha1(email.value + hex_sha1(password.value)), challenge.value)"');
    $this->addElement('header', 'header', 'login');
    $this->addElement('text', 'email', _('email'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('password', 'password', _('password'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('checkbox', 'remember', _('Remember me'), null, null);
    $this->addElement('hidden', 'challenge', (isset($_COOKIE['sess']) ? $_COOKIE['sess'] : $better_token));
    $this->addElement('submit', 'btn_Submit', _('Submit'), array('class' => 'submit'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('allow_hosts', 'function', 'aHosts', $this);
    $this->registerRule('authenticate_user', 'function', 'authenticateUser', $this);
    $this->addRule('email', _('email required'), 'required');
		$this->addRule('email', _('verify your email'), 'email');
    $this->addRule('email', _('remote host is not allowed to connect to this site'), 'allow_hosts');
    $this->addRule('email', _('Sign in failed'), 'authenticate_user');
    $this->addRule('password', _('password required'), 'required');
  }

  function aHosts() {
    $this->user->setLogin($this->getSubmitValue('login'));
    return $this->user->check_ahosts();
  }

  function authenticateUser() {
      $this->user->setEmail($this->getSubmitValue('email'));
      $this->user->setPassword($this->getSubmitValue('password'));
      $this->user->setRemember($this->getSubmitValue('remember'));
      $this->user->setForce_login($this->getSubmitValue('force'));
      return $this->user->authenticateUser();
  }

  function displayForm() {
    if($this->user->getForce_login()) {
      $this->tpl->assign('force', true);
      $this->addElement('checkbox', 'force', _('force login'), _('logged user will be disconnected'));
    }
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }

  function process() {
    header('Status: 303 See Other');     //help to avoid re-posting after login
    header("Cache-control: private");
    header('Location: /user');
    exit;
  }

} //end of class

?>